//
//  PayFoodViewController.m
//  Menu
//
//  Created by dllo on 15/7/7.
//  Copyright (c) 2015年 lijiangang. All rights reserved.
//

#import "PayFoodViewController.h"

@interface PayFoodViewController ()

@end

@implementation PayFoodViewController

- (void)dealloc
{
    [_topImageView release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.payFoodName;
    
    self.payKnowVC = [[PayFoodKnowViewController alloc] init];
    self.payKnowVC.view.frame = CGRectMake(0, (260+40)*HEIGHT+64, 375*WIDTH, 367*HEIGHT);
    self.payKnowVC.view.backgroundColor = [UIColor whiteColor];
    
    self.payStepVC = [[PayFoodStepViewController alloc] init];

    self.payStepVC.stepArray = [NSMutableArray array];

    self.payStepVC.view.frame = CGRectMake(0, (260+40)*HEIGHT+64, 375*WIDTH, 367*HEIGHT);

    self.payStepVC.view.backgroundColor = [UIColor whiteColor];
    
    self.payMaterialVC = [[PayFoodMaterialViewController alloc] init];
    self.payMaterialVC.view.frame = CGRectMake(0, (260+40)*HEIGHT+64, 375*WIDTH, 367*HEIGHT);
    self.payMaterialVC.view.backgroundColor = [UIColor whiteColor];
    
    [self addChildViewController:self.payKnowVC];
    [self addChildViewController:self.payStepVC];
    [self addChildViewController:self.payMaterialVC];
    [self.view addSubview:self.payKnowVC.view];
    [self.view addSubview:self.payStepVC.view];
    [self.view addSubview:self.payMaterialVC.view];
    
    UIImageView *place = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, 375*WIDTH, 260*HEIGHT)];
    place.image = [UIImage imageNamed:@"zhanwei.png"];
    [self.view addSubview:place];
    
    self.topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, 375*WIDTH, 260*HEIGHT)];
//    self.topImageView.backgroundColor = [UIColor lightGrayColor];
    self.topImageView.userInteractionEnabled = YES;
    [self.view addSubview:self.topImageView];
    
    [self loadData];
    [_topImageView release];
    
    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"所需材料",@"制作步骤",@"相关知识", nil];
    UISegmentedControl *segmentC = [[UISegmentedControl alloc] initWithItems:array];
    segmentC.frame = CGRectMake(0, 260*HEIGHT+64, 375*WIDTH, 40*HEIGHT);
    segmentC.tintColor = [UIColor lightGrayColor];
    segmentC.userInteractionEnabled = YES;
    segmentC.selectedSegmentIndex = 0;
    [segmentC addTarget:self action:@selector(payFoodSegmentFunction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segmentC];
    [segmentC release];
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"loading";
    [self.HUD show:YES];
    
    [_payKnowVC release];
    [_payStepVC release];
    [_payMaterialVC release];
}


-(void)loadData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://121.41.88.179:80/HandheldKitchen/api/more/hotwater!getHotwaterDetail.do?id=%@&is_traditional=0&phonetype=1",self.payFoodId];
    [AFNetWorkingYU netWorkingHanderGetGataWithUrl:urlStr resultBlock:^(id result)
    {
        NSString *imgUrl = [result objectForKey:@"imageFilename"];
        [self.topImageView sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
        NSMutableDictionary *bigDic = result;
        PayFoodModel *payFood = [[PayFoodModel alloc] init];
        [payFood setValuesForKeysWithDictionary:bigDic];
        NSMutableArray *processArray = [result objectForKey:@"TblProcess"];
        for (NSMutableDictionary *dic in processArray)
        {
            PayFoodModel *payFood = [[PayFoodModel alloc] init];
            [payFood setValuesForKeysWithDictionary:dic];
            [self.payStepVC.stepArray addObject:payFood];
        }
        self.payMaterialVC.payFood = payFood;
        self.payKnowVC.payFood = payFood;
        [self.payMaterialVC.materialTableView reloadData];
        [self.payStepVC.stepTableView reloadData];
        [self.payKnowVC.knowTableView reloadData];
        [self.HUD hide:YES];
    }];
}

-(void)payFoodSegmentFunction:(UISegmentedControl *)segment
{
    if (segment.selectedSegmentIndex == 0)
    {
        [self.view bringSubviewToFront:self.payMaterialVC.view];
    }
    if (segment.selectedSegmentIndex == 1)
    {
        [self.view bringSubviewToFront:self.payStepVC.view];
    }
    if (segment.selectedSegmentIndex == 2)
    {
        [self.view bringSubviewToFront:self.payKnowVC.view];
    }
}


































- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
